package com.chinanet.utils.webservice;

import org.apache.commons.lang.StringEscapeUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class WebService {

	/**
	 * Init WebService method
	 * @param attrsIn
	 * @return
	 */
	public String WebServiceInit(AttrsIn attrsIn) {
		System.out.println("========>>>");
		String returnBack = "";
		try {
			URL url = new URL(
					"http://134.96.104.121:8096/eai/services/EAIService");
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
			conn.setRequestMethod("POST");
			conn.setRequestProperty("soapAction", "");
			conn.connect();
			String soap = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
					+ "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:bus=\"http://businessmanager.eai.zjpii.com\">"
					+ "<soapenv:Header/>"
					+ "<soapenv:Body>"
					+ "<bus:requestHandle soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
					+ "<req_xml xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">"
					+ "<![CDATA[<Message><AreaName>天翼保障</AreaName>"
					+ "<ClientSysName>"+attrsIn.getClientSysName()+"</ClientSysName>"
					+ "<FunctionName>"+attrsIn.getFunctionName()+"</FunctionName>"
					+ "<RequestMessage>"+attrsIn.getRequestMessage()
					+ "</RequestMessage></Message>]]>" + "</req_xml>"
					+ "</bus:requestHandle>" + "</soapenv:Body>"
					+ "</soapenv:Envelope>";
			OutputStream out = conn.getOutputStream();
			out.write(soap.getBytes("UTF-8"));
			out.flush();
			int code = conn.getResponseCode();
			if (code == 200) {
				InputStream in = conn.getInputStream();
				byte b[] = new byte[10240];
				StringBuffer sb = new StringBuffer();
				int len = 0;
				while ((len = in.read(b)) != -1) {
					String str = new String(b, 0, len, "UTF-8");
					sb.append(str);
				}
				String ak = new String(sb.toString());
				ak = ak.replaceAll("&amp;", "");
				ak = ak.replaceAll("&lt;", "<");
				ak = ak.replaceAll("&gt;", ">");
				ak = ak.replaceAll("lt;", "<");
				ak = ak.replaceAll("gt;", ">");
				ak = ak.replaceAll("&quot;", "\"");
				ak = ak.replaceAll("&apos;", "\'");
				ak = ak.replaceAll(";", "");
				ak = ak.replaceAll("&#x", "\\\\u");
				returnBack = StringEscapeUtils.unescapeJava(ak);
				in.close();
			} else if (code == 500) {
				System.out.println("server ERROR!");
			} else if (code == 504) {
				System.out.println("Gateway Timeout!");
			} else if (code == 404) {
				System.out.println("Not Found!");
			} else if (code == 408) {
				System.out.println("Request Time-Out!");
			} else {
				System.out.println("Connection Failed!");
			}
			out.close();
			conn.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return returnBack;

	}

	/**
	 *  The content of the find nodes
	 * @param nodeName
	 * @param xml
	 * @return
	 */
	public String getPattern(String nodeName, String xml) {
		String returnBack = "";
		String regex = "<" + nodeName + ">([\\s\\S].*)</" + nodeName + ">";
		xml=xml.replaceAll("\\r", "");
		xml=xml.replaceAll("\\n", "");
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(xml);
		while (matcher.find()) {
			returnBack = matcher.group(1);
		}
		return dealkouhao(returnBack);
	}

	public String dealkouhao(String returnXml){
		returnXml=returnXml.replaceAll("\"","\'");
		if(returnXml.equals("null")){
			returnXml="没有数据";
		}
		return returnXml;
	}


	public static String getrandom(){
		return new SimpleDateFormat("yyyyMMddHHmm").format(new Date())+String.valueOf(generateRandomNumber(3));
	}
	public static String timeget(int i){
		if (i==0){
			return new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(new Date());
		}else {
			Calendar curr = Calendar.getInstance();
			curr.set(Calendar.DAY_OF_MONTH,curr.get(Calendar.DAY_OF_MONTH)-7);
			Date date=curr.getTime();
			return new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(date);
		}
	}
	private static  long generateRandomNumber(int n){
		if(n<1){
			throw new IllegalArgumentException("随机数位数必须大于0");
		}
		return (long)(Math.random()*9*Math.pow(10,n-1)) + (long)Math.pow(10,n-1);
	}
//
//	public static void main(String[] args){
//		System.out.println(timeget(0));
//	}

}
